From f93506aff274ecff9404ba1690aad6bd6afa61b6 Mon Sep 17 00:00:00 2001 From: Tomas Sedovic Date: Tue, 28 Oct 2014 09:31:34 +0100 Subject: [PATCH] Add `--name` to `cargo test` and `cargo bench` You can now run a single test/bench file by passing its name to `cargo test` or `cargo bench`. --- src/bin/bench.rs | 3 + src/bin/test.rs | 3 + src/cargo/ops/cargo_rustc/compilation.rs | 2 +- src/cargo/ops/cargo_rustc/fingerprint.rs | 2 +- src/cargo/ops/cargo_test.rs | 10 +++- tests/test_cargo_bench.rs | 41 +++++++++++++ tests/test_cargo_test.rs | 75 ++++++++++++++++++++++++ 7 files changed, 133 insertions(+), 3 deletions(-) diff --git a/src/bin/bench.rs b/src/bin/bench.rs index 53fb6e3bf..56e441cfa 100644 --- a/src/bin/bench.rs +++ b/src/bin/bench.rs @@ -11,6 +11,7 @@ struct Options { flag_package: Option, flag_jobs: Option, flag_features: Vec, + flag_name: Option, flag_no_default_features: bool, flag_target: Option, flag_manifest_path: Option, @@ -26,6 +27,7 @@ Usage: Options: -h, --help Print this message + --name NAME Name of the bench to run --no-run Compile, but don't run benchmarks -p SPEC, --package SPEC Package to run benchmarks for -j N, --jobs N The number of jobs to run in parallel @@ -50,6 +52,7 @@ pub fn execute(options: Options, shell: &mut MultiShell) -> CliResult shell.set_verbose(options.flag_verbose); let mut ops = ops::TestOptions { + name: options.flag_name.as_ref().map(|s| s.as_slice()), no_run: options.flag_no_run, compile_opts: ops::CompileOptions { env: "bench", diff --git a/src/bin/test.rs b/src/bin/test.rs index c4a24aa4b..0dd4d4c30 100644 --- a/src/bin/test.rs +++ b/src/bin/test.rs @@ -11,6 +11,7 @@ struct Options { flag_features: Vec, flag_jobs: Option, flag_manifest_path: Option, + flag_name: Option, flag_no_default_features: bool, flag_no_run: bool, flag_package: Option, @@ -26,6 +27,7 @@ Usage: Options: -h, --help Print this message + --name NAME Name of the test to run --no-run Compile, but don't run tests -p SPEC, --package SPEC Package to run tests for -j N, --jobs N The number of jobs to run in parallel @@ -49,6 +51,7 @@ pub fn execute(options: Options, shell: &mut MultiShell) -> CliResult shell.set_verbose(options.flag_verbose); let mut ops = ops::TestOptions { + name: options.flag_name.as_ref().map(|s| s.as_slice()), no_run: options.flag_no_run, compile_opts: ops::CompileOptions { env: "test", diff --git a/src/cargo/ops/cargo_rustc/compilation.rs b/src/cargo/ops/cargo_rustc/compilation.rs index 8e8ddf695..ebeeefe1f 100644 --- a/src/cargo/ops/cargo_rustc/compilation.rs +++ b/src/cargo/ops/cargo_rustc/compilation.rs @@ -15,7 +15,7 @@ pub struct Compilation { pub libraries: HashMap>, /// An array of all tests created during this compilation. - pub tests: Vec, + pub tests: Vec<(String, Path)>, /// An array of all binaries created. pub binaries: Vec, diff --git a/src/cargo/ops/cargo_rustc/fingerprint.rs b/src/cargo/ops/cargo_rustc/fingerprint.rs index 64866ab3a..876a58fb6 100644 --- a/src/cargo/ops/cargo_rustc/fingerprint.rs +++ b/src/cargo/ops/cargo_rustc/fingerprint.rs @@ -100,7 +100,7 @@ pub fn prepare_target(cx: &mut Context, pkg: &Package, target: &Target, pairs.push((old_root.join(filename), root.join(filename))); if target.get_profile().is_test() { - cx.compilation.tests.push(dst.clone()); + cx.compilation.tests.push((target.get_name().into_string(), dst.clone())); } else if target.is_bin() { cx.compilation.binaries.push(dst.clone()); } else if target.is_lib() { diff --git a/src/cargo/ops/cargo_test.rs b/src/cargo/ops/cargo_test.rs index 387b32751..426fe9f80 100644 --- a/src/cargo/ops/cargo_test.rs +++ b/src/cargo/ops/cargo_test.rs @@ -8,6 +8,7 @@ use util::{CargoResult, ProcessError}; pub struct TestOptions<'a> { pub compile_opts: ops::CompileOptions<'a>, pub no_run: bool, + pub name: Option<&'a str>, } pub fn run_tests(manifest_path: &Path, @@ -20,8 +21,13 @@ pub fn run_tests(manifest_path: &Path, if options.no_run { return Ok(None) } compile.tests.sort(); + let target_name = options.name; + let mut tests_to_run = compile.tests.iter().filter(|&&(ref test_name, _)| { + target_name.map_or(true, |target_name| target_name == test_name.as_slice()) + }); + let cwd = os::getcwd(); - for exe in compile.tests.iter() { + for &(_, ref exe) in tests_to_run { let to_display = match exe.path_relative_from(&cwd) { Some(path) => path, None => exe.clone(), @@ -39,6 +45,8 @@ pub fn run_tests(manifest_path: &Path, } } + if options.name.is_some() { return Ok(None) } + if options.compile_opts.env == "bench" { return Ok(None) } let mut libs = compile.package.get_targets().iter().filter_map(|target| { diff --git a/tests/test_cargo_bench.rs b/tests/test_cargo_bench.rs index d1d4da795..a661d795e 100644 --- a/tests/test_cargo_bench.rs +++ b/tests/test_cargo_bench.rs @@ -50,6 +50,47 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured RUNNING))); }) +test!(bench_target_name { + let prj = project("foo") + .file("Cargo.toml" , r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + + [[bin]] + name="bin1" + path="src/bin1.rs" + + [[bin]] + name="bin2" + path="src/bin2.rs" + "#) + .file("src/bin1.rs", r#" + extern crate test; + #[bench] fn run1(_ben: &mut test::Bencher) { }"#) + .file("src/bin2.rs", r#" + extern crate test; + #[bench] fn run2(_ben: &mut test::Bencher) { }"#); + + let expected_stdout = format!("\ +{compiling} foo v0.0.1 ({dir}) +{runnning} target[..]release[..]bin2[..] + +running 1 test +test run2 ... bench: 0 ns/iter (+/- 0) + +test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured + +", + compiling = COMPILING, + runnning = RUNNING, + dir = prj.url()); + + assert_that(prj.cargo_process("bench").arg("--name").arg("bin2"), + execs().with_status(0).with_stdout(expected_stdout.as_slice())); +}) + test!(cargo_bench_verbose { let p = project("foo") .file("Cargo.toml", basic_bin_manifest("foo").as_slice()) diff --git a/tests/test_cargo_test.rs b/tests/test_cargo_test.rs index 34c045449..901499c44 100644 --- a/tests/test_cargo_test.rs +++ b/tests/test_cargo_test.rs @@ -867,6 +867,81 @@ test!(test_no_run { dir = p.url()).as_slice())); }) +test!(test_run_specific_bin_target { + let prj = project("foo") + .file("Cargo.toml" , r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + + [[bin]] + name="bin1" + path="src/bin1.rs" + + [[bin]] + name="bin2" + path="src/bin2.rs" + "#) + .file("src/bin1.rs", "#[test] fn test1() { }") + .file("src/bin2.rs", "#[test] fn test2() { }"); + + let expected_stdout = format!("\ +{compiling} foo v0.0.1 ({dir}) +{running} target[..]bin2-[..] + +running 1 test +test test2 ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured + +", + compiling = COMPILING, + running = RUNNING, + dir = prj.url()); + + assert_that(prj.cargo_process("test").arg("--name").arg("bin2"), + execs().with_status(0).with_stdout(expected_stdout.as_slice())); +}) + +test!(test_run_specific_test_target { + let prj = project("foo") + .file("Cargo.toml" , r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + "#) + .file("src/bin/a.rs", "fn main() { }") + .file("src/bin/b.rs", "#[test] fn test_b() { } fn main() { }") + .file("tests/a.rs", "#[test] fn test_a() { }") + .file("tests/b.rs", "#[test] fn test_b() { }"); + + let expected_stdout = format!("\ +{compiling} foo v0.0.1 ({dir}) +{running} target[..]b-[..] + +running 1 test +test test_b ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured + +{running} target[..]b-[..] + +running 1 test +test test_b ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured + +", + compiling = COMPILING, + running = RUNNING, + dir = prj.url()); + + assert_that(prj.cargo_process("test").arg("--name").arg("b"), + execs().with_status(0).with_stdout(expected_stdout.as_slice())); +}) + test!(test_no_harness { let p = project("foo") .file("Cargo.toml", r#" -- 2.30.2